//
//  HelpCenterViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/5.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "HelpCenterViewController.h"
#import "ServiceCharge.h"
#import "AppDelegate.h"
//帮助中心Model
#import "MineHelpCenterModelPack.h"

#define WIDTH  [[UIScreen mainScreen]bounds].size.width

#define HEIGHT [[UIScreen mainScreen]bounds].size.height

@interface HelpCenterViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)ServiceCharge *serviceCharge;
@property(nonatomic,strong)NSMutableArray *helpCenterModelArray;

@end

@implementation HelpCenterViewController

-(NSMutableArray *)helpCenterModelArray
{
    if (_helpCenterModelArray == nil) {
        _helpCenterModelArray = [NSMutableArray array];
    }
    return _helpCenterModelArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"帮助中心";
    self.view.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:244 / 255.0 blue:246 / 255.0 alpha:1.0];
    //创建tableView
    [self createTableViee];
    //获取帮助列表
    [self getHelpCenterList];
}

-(void)getHelpCenterList
{
    MineHelpCenterRequest *request = [[MineHelpCenterRequest alloc] init];
    request.type = @"4";
    request.pageNum = @"1";
    request.pageSize = @"500";
    request.keyword = @"";
    NSDictionary *dic = [request mj_keyValues];
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/help/helpListF0200"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:dic HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            [self dealHelpCenterList:responseObject];
        }
        
    } FailedHandler:^(NSError *error) {
        
    }];
}
-(void)dealHelpCenterList:(id)responseObject
{
    MineHelpCenterResponse *mineHelpCenterResponse = [MineHelpCenterResponse mj_objectWithKeyValues:responseObject];
    for (MineHelpCenterRecord *record in mineHelpCenterResponse.response.list) {
        [self.helpCenterModelArray addObject:record];
    }
    [self.tableView reloadData];
}


-(void)createTableViee
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.helpCenterModelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    MineHelpCenterRecord *record = self.helpCenterModelArray[indexPath.row];
    
    cell.textLabel.text = record.conTitle;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    self.tableView.userInteractionEnabled = NO;
    MineHelpCenterRecord *record = self.helpCenterModelArray[indexPath.row];
    [self serviceChargeWIthType:indexPath.row AndModel:record];
}

- (void)serviceChargeWIthType:(NSInteger)viewType AndModel:(MineHelpCenterRecord *)record{
//    if (!_serviceCharge) {
        self.serviceCharge = [[ServiceCharge alloc] initWithFrame:(CGRectMake(0, HEIGHT, WIDTH, 400)) andType:viewType AndModel:record];
        //给弹出框加上点击事件  响应底部弹窗消失
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesturedDetected:)];
        [self.serviceCharge addGestureRecognizer:tapGesture];
        
        //点击退出弹框
        [self.serviceCharge.offBtn addTarget:self action:@selector(offBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        
//    }
    AppDelegate *delegate  = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:_serviceCharge];
    
    
    
    __weak HelpCenterViewController *weakSelf = self;
    self.serviceCharge.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    [UIView animateWithDuration:0.5 animations:^{
        [weakSelf.serviceCharge setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    } completion:^(BOOL finished) {
        self.tableView.userInteractionEnabled = YES;
        weakSelf.serviceCharge.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    }];
}
-(void)offBtnAction
{
    self.serviceCharge.backgroundColor = [UIColor clearColor];
    __weak ServiceCharge *weakSelf = self.serviceCharge;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.frame = CGRectMake(0, HEIGHT, WIDTH, HEIGHT);
    } completion:^(BOOL finished) {
        //        weakSelf.payMentView.frame = CGRectMake(WIDTH, 0, WIDTH, payViewHeight);
    }];
}

-(void)tapGesturedDetected:(UIGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self.serviceCharge];
    
    if (point.y < HEIGHT - 400) {
        self.serviceCharge.backgroundColor = [UIColor clearColor];
        __weak ServiceCharge *weakSelf = self.serviceCharge;
        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.frame = CGRectMake(0, HEIGHT, WIDTH, HEIGHT);
        } completion:^(BOOL finished) {
        }];
    }
}


@end
